package com.czxy.candidates.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.candidates.feign.OccupationFeign;
import com.czxy.candidates.feign.UserFeign;
import com.czxy.domain.TbCandidates;
import com.czxy.candidates.mapper.CandidatesMapper;
import com.czxy.candidates.service.CandidatesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.domain.TbOccupation;
import com.czxy.vo.CandidatesVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 求职者信息表 服务实现类
 * </p>
 *
 * @author xuewen
 * @since 2022-01-13
 */
@Service
public class CandidatesServiceImpl extends ServiceImpl<CandidatesMapper, TbCandidates> implements CandidatesService {

    @Autowired
    private UserFeign userFeign;
    @Autowired
    private OccupationFeign occupationFeign;
    @Override
    public Page condition(CandidatesVo candidatesVo) {
        System.out.println(candidatesVo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(candidatesVo.getName()),"name",candidatesVo.getName());
        queryWrapper.like(StringUtils.isNotBlank(candidatesVo.getPhone()),"phone",candidatesVo.getPhone());
        queryWrapper.eq(StringUtils.isNotBlank(candidatesVo.getGender()),"gender",candidatesVo.getGender());
        queryWrapper.eq(StringUtils.isNotBlank(candidatesVo.getCityId()),"city_id",candidatesVo.getCityId());
        queryWrapper.eq(StringUtils.isNotBlank(candidatesVo.getOccupationId()),"occupation_id",candidatesVo.getOccupationId());
        Page<TbCandidates> page = new Page(candidatesVo.getCurrent(),candidatesVo.getSize());
        this.baseMapper.selectPage(page,queryWrapper);
        for (TbCandidates tbCandidates : page.getRecords()) {
            if (tbCandidates.getOccupationId()!=null){
                TbOccupation data = occupationFeign.findById(tbCandidates.getOccupationId()).getData();
                tbCandidates.setTbOccupation(data);
            }
            tbCandidates.setTbUser(userFeign.findByCandidateId(tbCandidates.getId()).getData());
        }
        return page;
    }

    @Override
    public List<TbCandidates> findAll() {
        List<TbCandidates> list = this.baseMapper.selectList(null);
        for (TbCandidates tbCandidates : list) {
            if (tbCandidates.getOccupationId()!=null){
                TbOccupation data = occupationFeign.findById(tbCandidates.getOccupationId()).getData();
                tbCandidates.setTbOccupation(data);
            }
            tbCandidates.setTbUser(userFeign.findByCandidateId(tbCandidates.getId()).getData());
        }
        return list;
    }
}
